﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DB.SqlHelper;
using Framework.Common.Common;
using Mode;
using System.Net.Mail;

namespace Framework.DataAccess
{
    class SubjectMessage : System.Net.Mail.MailMessage
    {
        public int candidate { get; set; }
    }

    public class email
    {
        private DbInstance db;
        private string smtp_from;
        private string exam_host;
        private string smtp_password;
        private string smtp_host;
        private int smtp_port;
        private int examid;
        private bool issend;

        // 构造函数,传入考场ID.
        public email( int examID ) 
        {
            db = DbManager.GetDbInstance();
            this.examid = examID;
            get_smtp();
        }

        // 取得等发件人
        public void SendMail()
        {
            if (!issend) return;

            string sql = " select  ";
            sql += "candidate_table.id as 'id' , ";
            sql += " member_view.M_NAME as 'm_name', ";
            sql += " member_view.A_NAME as 'a_name', ";
            sql += " member_view.R_NAME as 'r_name', ";
            sql += " examination_table.[begin] as 'e_begin', ";
            sql += " examination_table.[end] as 'e_end', ";
            sql += " examination_table.[name] as 'e_name', ";
            sql += " member_view.M_EMAIL as 'email' ";
            sql += " from candidate_table  ";
            sql += " join member_view on member_view.M_ID = candidate_table.member ";
            sql += " join examination_table on examination_table.id = candidate_table.examination ";
            sql += " where examination = " + examid;

            DataTable dt = db.ExecuteDataTableBySql(sql);
            foreach (DataRow row in dt.Rows)
            {
                SubjectMessage msg = new SubjectMessage();
                msg.To.Add(row["email"].ToStr());
                msg.Subject = row["e_name"].ToStr() + " 考试通知";
                msg.Body = String.Format("{0}:您好.\r\n"
                    +"{1} {2}于{3}至{4}举行{5}"
                    +"考试.考试站点请登陆{6}.",
                    row["m_name"], row["a_name"], row["r_name"],
                    row["e_begin"],
                    row["e_end"],
                    row["e_name"], this.exam_host );
                msg.candidate = row["id"].ToInt();
                msg.From = new MailAddress(this.smtp_from);
                msg.IsBodyHtml = true;
                msg.BodyEncoding = System.Text.Encoding.UTF8;
                msg.Priority = System.Net.Mail.MailPriority.Normal;

                sendasync(msg);
            }
        }

        // 取得SMTP邮件服务器信息
        private void get_smtp()
        {
            DataTable dt = db.ExecuteDataTableBySql("select * from information_table where [key] like 'smtp_%' or [key]='exam_host' or [key]='issend' ");
            foreach (DataRow row in dt.Rows)
            {
                string key = row["key"].ToStr();
                switch ( key )
                {
                    case "smtp_password":
                        this.smtp_password = row["value"].ToStr();
                        break;
                    case "smtp_host":
                        this.smtp_host = row["value"].ToStr();
                        break;
                    case "smtp_port":
                        this.smtp_port = row["value"].ToInt();
                        break;
                    case "smtp_email":
                        this.smtp_from = row["value"].ToStr();
                        break;
                    case "exam_host":
                        this.exam_host = row["value"].ToStr();
                        break;
                    case "issend":
                        this.issend = row["value"].ToBool();
                        break;
                }
            }
        }


        private void sendasync(SubjectMessage message)
        {
            if (issend)
            {
                System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();
                sc.Credentials = new System.Net.NetworkCredential(message.From.Address, this.smtp_password);
                sc.Host = smtp_host;
                sc.Port = smtp_port;

                //sc.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(finish);
                try
                {
                    //sc.SendAsync(message, message.candidate);
                    sc.Send(message);

                    DbInstance tdb = DbManager.GetDbInstance();
                    string sql = String.Format("update candidate_table set notification=1 where id = " + message.candidate);

                    try { tdb.ExecuteNonQueryBySql(sql); }
                    catch (Exception) { }
                }
                catch (Exception)
                { }
            }
        }

    }
}
